/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.generator;

import java.lang.reflect.Method;

import org.sourceprojects.lycia.DefinitionBuilder;
import org.sourceprojects.lycia.Delegate;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.ParserContext;
import org.sourceprojects.lycia.QualifiedName;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.internal.validation.ConstraintValidatorUtils;
import org.sourceprojects.lycia.internal.validation.PropertyDescriptor;
import org.sourceprojects.lycia.internal.validation.ValidatingTagDefinitionParser;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

class AnnotatedParameterReflectiveDelegate<B> implements Delegate<B>,
		ValidatingTagDefinitionParser<B> {

	private final LyciaParser<B> lyciaParser;
	private final Object parser;
	private final Method method;
	private final PropertyDescriptor<B>[] propertyDescriptors;

	AnnotatedParameterReflectiveDelegate(final Object parser,
			final Method method, final LyciaParser<B> lyciaParser) {

		this.lyciaParser = lyciaParser;
		this.parser = parser;
		this.method = method;

		this.propertyDescriptors = ConstraintValidatorUtils
				.buildPropertyDescriptors(method, lyciaParser);
	}

	@Override
	public void invokeParse(final Element element, final String localName,
			final QualifiedName parent, final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {

		try {
			final Object[] parameters = MethodParameterUtils
					.generateParameter(method, element, localName,
							parserContext, definitionBuilder, lyciaParser);

			method.invoke(parser, parameters);

		} catch (final Exception e) {
			throw new LyciaParsingException("Parser method " + method.getName()
					+ " could not be executed", e);
		}
	}

	@Override
	public Node invokeTransform(final Element element, final String localName,
			final QualifiedName parent, final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {

		try {
			final Object[] parameters = MethodParameterUtils
					.generateParameter(method, element, localName,
							parserContext, definitionBuilder, lyciaParser);

			return (Node) method.invoke(parser, parameters);

		} catch (final Exception e) {
			throw new LyciaParsingException("Transformer method "
					+ method.getName() + " could not be executed", e);
		}
	}

	@Override
	public String toString() {
		return "ReflectiveDelegate [method=" + method + ", parser=" + parser
				+ "]";
	}

	@Override
	public PropertyDescriptor<B>[] getPropertyDescriptor() {
		return propertyDescriptors;
	}

}
